"""
Pydantic 数据模型

定义 API 请求和响应的数据结构
"""

from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any
from datetime import datetime


# ========== 任务相关模型 ==========

class TaskCreate(BaseModel):
    """创建任务请求"""
    task_name: str = Field(..., description="任务名称")
    args: List[Any] = Field(default=[], description="任务参数列表")
    kwargs: Dict[str, Any] = Field(default={}, description="任务关键字参数")
    queue: Optional[str] = Field(None, description="任务队列")
    countdown: Optional[int] = Field(None, description="延迟执行（秒）")
    eta: Optional[datetime] = Field(None, description="指定执行时间")


class TaskResponse(BaseModel):
    """任务响应模型"""
    task_id: str = Field(..., description="任务 ID")
    task_name: str = Field(..., description="任务名称")
    status: str = Field(..., description="任务状态")
    queue: Optional[str] = Field(None, description="任务队列")
    

class TaskResult(BaseModel):
    """任务结果模型"""
    task_id: str = Field(..., description="任务 ID")
    status: str = Field(..., description="任务状态")
    result: Optional[Any] = Field(None, description="任务结果")
    error: Optional[str] = Field(None, description="错误信息")
    traceback: Optional[str] = Field(None, description="错误堆栈")
    started_at: Optional[datetime] = Field(None, description="开始时间")
    completed_at: Optional[datetime] = Field(None, description="完成时间")
    runtime: Optional[float] = Field(None, description="运行时长（秒）")


class TaskProgress(BaseModel):
    """任务进度模型"""
    task_id: str = Field(..., description="任务 ID")
    status: str = Field(..., description="任务状态")
    current: Optional[int] = Field(None, description="当前进度")
    total: Optional[int] = Field(None, description="总数")
    percent: Optional[int] = Field(None, description="百分比")
    message: Optional[str] = Field(None, description="进度消息")


# ========== 邮件相关模型 ==========

class EmailRequest(BaseModel):
    """发送邮件请求"""
    to_email: str = Field(..., description="收件人邮箱")
    subject: str = Field(..., description="邮件主题")
    body: str = Field(..., description="邮件正文")
    html: bool = Field(default=False, description="是否为 HTML 格式")


class BulkEmailRequest(BaseModel):
    """批量发送邮件请求"""
    emails: List[EmailRequest] = Field(..., description="邮件列表")


# ========== 文件处理相关模型 ==========

class ImageProcessRequest(BaseModel):
    """图片处理请求"""
    image_path: str = Field(..., description="图片路径")
    operations: List[str] = Field(..., description="操作列表")


class ThumbnailRequest(BaseModel):
    """缩略图生成请求"""
    image_path: str = Field(..., description="图片路径")
    sizes: List[str] = Field(
        default=["small", "medium", "large"],
        description="尺寸列表"
    )


# ========== 报表相关模型 ==========

class ReportRequest(BaseModel):
    """报表生成请求"""
    report_type: str = Field(..., description="报表类型")
    start_date: str = Field(..., description="开始日期（YYYY-MM-DD）")
    end_date: str = Field(..., description="结束日期（YYYY-MM-DD）")


# ========== 任务链和组 ==========

class ChainTaskRequest(BaseModel):
    """任务链请求"""
    data: str = Field(..., description="初始数据")


class GroupTaskRequest(BaseModel):
    """任务组请求"""
    item_ids: List[int] = Field(..., description="项目 ID 列表")


# ========== 任务统计 ==========

class TaskStats(BaseModel):
    """任务统计"""
    total: int = Field(..., description="总任务数")
    active: int = Field(..., description="活跃任务数")
    scheduled: int = Field(..., description="计划任务数")
    reserved: int = Field(..., description="保留任务数")
    succeeded: int = Field(..., description="成功任务数")
    failed: int = Field(..., description="失败任务数")


class WorkerInfo(BaseModel):
    """Worker 信息"""
    name: str = Field(..., description="Worker 名称")
    status: str = Field(..., description="状态")
    active_tasks: int = Field(..., description="活跃任务数")
    processed_tasks: int = Field(..., description="已处理任务数")


# ========== 定时任务相关 ==========

class ScheduledTask(BaseModel):
    """定时任务信息"""
    name: str = Field(..., description="任务名称")
    task: str = Field(..., description="任务函数")
    schedule: str = Field(..., description="调度规则")
    enabled: bool = Field(..., description="是否启用")
    last_run: Optional[datetime] = Field(None, description="上次运行时间")
    next_run: Optional[datetime] = Field(None, description="下次运行时间")


# ========== 通用响应 ==========

class MessageResponse(BaseModel):
    """通用消息响应"""
    message: str = Field(..., description="响应消息")
    data: Optional[Any] = Field(None, description="额外数据")

